package net.cathackers.devel.scmp.config.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.config.interfaces.DBConfig;
import net.cathackers.devel.scmp.factories.SCMPFactory;

public class XMLDBConfig implements DBConfig {
    protected Properties backend_;
    protected final String DB_NAME = "dbName";
    protected final String DB_HOST = "host";
    protected final String DB_TABLE = "table";
    protected final String DB_USER = "username";
    protected final String DB_PASS = "password";
    private Logger logger_;

    public XMLDBConfig() {
	backend_ = new Properties();
	logger_ = SCMPFactory.getLogger(this);
    }

    public XMLDBConfig(Properties backend) {
	backend_ = backend;
	logger_ = SCMPFactory.getLogger(this);
    }

    public XMLDBConfig(String configFile) {
	backend_ = new Properties();
	try {
	    this.loadFromXML(configFile);
	} catch (FileNotFoundException e) {
	    logger_.error("file not found");
	    e.printStackTrace();
	} catch (IOException e) {
	   logger_.error("IO Exception ");
	    e.printStackTrace();
	}
	logger_ = SCMPFactory.getLogger(this);
    }

    public XMLDBConfig(String dbLocation, String dbName, String dbTableName,
	    String dbUsername, String dbPassword) {
	logger_ = SCMPFactory.getLogger(this);
	 backend_ = new Properties();
	setHostname(dbLocation);
	setDBName(dbName);
	setTableName(dbTableName);
	setUsername(dbUsername);
	setPassword(dbPassword);
    }
    public Properties getBackend() {
	return backend_;
    }

    @Override
    public String getDBName() {
	return backend_.getProperty(DB_NAME);
    }

    @Override
    public String getHostname() {
	return backend_.getProperty(DB_HOST);
    }

    @Override
    public String getPassword() {
	return backend_.getProperty(DB_PASS);
    }

    @Override
    public String getTableName() {
	return backend_.getProperty(DB_TABLE);
    }

    @Override
    public String getUsername() {
	return backend_.getProperty(DB_USER);
    }

    @Override
    public void setDBName(String dbName) {
	backend_.setProperty(DB_NAME, dbName);
    }

    @Override
    public void setHostname(String dbLocation) {
	backend_.setProperty(DB_HOST, dbLocation);
    }

    @Override
    public void setPassword(String dbPassword) {
	backend_.setProperty(DB_PASS, dbPassword);
    }

    @Override
    public void setTableName(String dbTableName) {
	backend_.setProperty(DB_TABLE, dbTableName);
    }

    @Override
    public void setUsername(String dbUsername) {
	backend_.setProperty(DB_USER, dbUsername);
    }

    @Override
    public void loadFromXML(String filename) throws FileNotFoundException,
	    IOException {
	try {
	    backend_.loadFromXML(new FileInputStream(filename));
	} catch (FileNotFoundException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	} catch (IOException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	}
    }

    @Override
    public void saveAsXML(String filename) throws FileNotFoundException,
	    IOException {
	try {
	    backend_.storeToXML(new FileOutputStream(filename),
		    "Automatically Saved by The System");

	} catch (FileNotFoundException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	} catch (IOException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	}

    }

    @Override
    public String getAuthType() {
	return backend_.getProperty(AUTH_TYPE, "mysql");
    }

    @Override
    public void setAuthType(String authType) {
	backend_.setProperty(AUTH_TYPE, authType);
    }

}
